﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using Infrastructure.Managers;
using B10_Ex02_EladLebovitch_034695114.Interfaces;

namespace B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel
{
    public delegate void EnemyDestroyed(object sender, EventArgs e);

    public class EnemySpriteBase : Sprite, ICollidable2D, IEnemyObjects, IHaveScore
    {
        public event EnemyDestroyed Destroyed;
        public bool m_IsDying = false;
        
        protected virtual double ShrinkSeconds
        { 
            get;
            set; 
        }

        public int Points 
        { 
            get; 
            set; 
        }

        public override void Update(GameTime gameTime)
        {
            // Check if this sprite was hit
            if (m_IsDying)
            {
                this.Shrink(gameTime, ShrinkSeconds);
                
                // If we finished shrinking
                if (this.Scale <= 0)
                {
                    // We are dead
                    this.Distroy();
                    NotifyDestruction();
                }
            }
            else
            {
                base.Update(gameTime);
            }
        }

        public EnemySpriteBase(string i_AssetName, Game i_Game, int i_UpdateOrder, int i_DrawOrder)
            : base(i_AssetName, i_Game, i_UpdateOrder, i_DrawOrder)
        {
            ((ICollisionsManager)i_Game.Services.GetService(typeof(ICollisionsManager))).AddObjectToMonitor(this);
        }

        public EnemySpriteBase(string i_AssetName, Game i_Game, int i_CallsOrder)
            : base(i_AssetName, i_Game, i_CallsOrder)
        { 
        }

        public EnemySpriteBase(string i_AssetName, Game i_Game)
            : base(i_AssetName, i_Game, int.MaxValue)
        { 
        }

        public EnemySpriteBase(string i_AssetName, int pointsPerEnemy, Game i_Game)
            : base(i_AssetName, i_Game, int.MaxValue)
        {
            this.Points = pointsPerEnemy;
        }

        protected void NotifyDestruction()
        {
            // Notify that it was destoryed
            if (this.Destroyed != null)
            {
                Destroyed(this, new EventArgs());
            }
        }
    }
}
